package com.avic.rong.questionOrAnswer;




import android.app.ProgressDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.avic.rong.R;


/**
 * 自定义ProgressDialog显示样式
 * created by xtj on 2017/12/6 17:31.
 */
public class CustomProgressDialog extends ProgressDialog {
    private static CustomProgressDialog customProgressDialog = null;

    public CustomProgressDialog(Context context){
        super(context);
    }

    public CustomProgressDialog(Context context, int theme) {
        super(context, theme);
    }
    /**
     * @param context
     * @return
     */
    public static CustomProgressDialog createDialog(Context context){
        if(customProgressDialog != null){
            customProgressDialog = null;
        }
        customProgressDialog = new CustomProgressDialog(context, R.style.CustomProgressDialog);
        try {
            if(!customProgressDialog.isShowing()){
                customProgressDialog.show();
            }
            View v = LayoutInflater.from(context).inflate(R.layout.dialog_custom_progress, null);
            //改成长宽自适应
//			LinearLayout llCover = (LinearLayout) v.findViewById(R.id.llCover);
//			LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
//			if(width == 0){
//				//设置dialog的宽高为屏幕宽度的1/3
//				layoutParams.width = DeviceInfoUtil.getScreenWidth(context)/3;
//			}else {
//				layoutParams.width = width;
//			}
//			layoutParams.height = layoutParams.width;
//			llCover.setLayoutParams(layoutParams);
            //一定要在 customProgressDialog.show() 之后设置
            customProgressDialog.setContentView(v);

        } catch (Exception e) {
            e.printStackTrace();
        }
        startAnimation();
        return customProgressDialog;
    }


    public static void startAnimation(){
        if (customProgressDialog == null){
            return;
        }
//        ImageView imageView = (ImageView) customProgressDialog.findViewById(R.id.load_animi);
//        if(imageView == null){
//            return;
//        }
//        AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
//        animationDrawable.start();
    }

    /**
     *
     * 标题
     * @param strTitle
     * @return
     *
     */
    public CustomProgressDialog setTitile(String strTitle){
        return customProgressDialog;
    }

    /**
     * 提示内容
     * @param strMessage
     * @return
     *
     */
    public CustomProgressDialog setMessage(String strMessage){
        TextView tvMsg = (TextView)customProgressDialog.findViewById(R.id.id_tv_loadingmsg);

        if (tvMsg != null){
            tvMsg.setText(strMessage);
        }

        return customProgressDialog;
    }

}

